package hz.mobileproject.pda.ui.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.toolsmanager.bean.response.RP_LoadCallList;

import static com.lzy.okgo.utils.HttpUtils.runOnUiThread;

/**
 * Created by Lc on 2018/4/27.
 */

public abstract class BaseCustomExpandableLvAdapter<P extends ParentBean<C>, C, PV, CV> extends BaseExpandableListAdapter {
    protected Context mContext;
    protected List<P> mParentList;
    protected LayoutInflater mInflater;

    public BaseCustomExpandableLvAdapter(Context mContext) {
        this.mContext = mContext;
        mParentList = new ArrayList<>();
        mInflater = LayoutInflater.from(mContext);
    }


    @Override
    public int getGroupCount() {
        return mParentList == null ? 0 : mParentList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return mParentList.get(i).getChildrenList() == null ? 0 : mParentList.get(i).getChildrenList().size();
    }

    @Override
    public P getGroup(int i) {
        return mParentList.get(i);
    }

    @Override
    public C getChild(int i, int i1) {
        return mParentList.get(i).getChildrenList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return getCombinedChildId(i, i1);
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        PV parentVH;
        if (view == null) {
            view = mInflater.inflate(bindParentView(), null);
            parentVH = createParentViewHolder();
            x.view().inject(parentVH, view);
            view.setTag(parentVH);
        } else {
            parentVH = (PV) view.getTag();
        }
        bindParentData(i, view, viewGroup, mParentList.get(i), parentVH);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        CV childrenVH;
        if (view == null) {
            view = mInflater.inflate(bindChildrenView(), null);
            childrenVH = createChildrenViewHolder();
            x.view().inject(childrenVH, view);
            view.setTag(childrenVH);
        } else {
            childrenVH = (CV) view.getTag();
        }
        bindChildrenData(i, i1, view, viewGroup, mParentList.get(i).getChildrenList().get(i1), childrenVH);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }


    /*
     *全部展开
     */
    public void expandAllGroup(ExpandableListView expandableListView) {
        for (int i = 0; i < getGroupCount(); i++) {
            expandableListView.expandGroup(i);
        }
    }

    public void clearAdd(List<P> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mParentList.clear();
        this.mParentList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearAdd(P t) {
        this.mParentList.clear();
        if (t != null) {
            this.mParentList.add(t);
        }
        notifyDataSetChanged();
    }

    public void add(List<P> list) {
        if (list == null) {
            return;
        }
        this.mParentList.addAll(list);
        notifyDataSetChanged();
    }

    public void add(P item) {
        if (item == null) {
            return;
        }
        this.mParentList.add(item);
        notifyDataSetChanged();
    }

    public void deleteOne(int position) {
        if (position < 0 || position >= mParentList.size()) {
            return;
        }
        mParentList.remove(position);
        notifyDataSetChanged();
    }

    public void deleteOne(P p) {
        mParentList.remove(p);
    }

    public void deleteOneChildren(int position1, int position2) {
        if (position1 < 0 || position1 >= mParentList.size()) {
            return;
        }
        P p = mParentList.get(position1);
        if (position2 < 0 || position2 >= p.getChildrenList().size()) {
            return;
        }
        p.getChildrenList().remove(position2);
        notifyDataSetChanged();
    }

    public void deleteOneChildren(int position1, C c) {
        if (position1 < 0 || position1 >= mParentList.size()) {
            return;
        }
        P p = mParentList.get(position1);
        p.getChildrenList().remove(c);
        notifyDataSetChanged();
    }

    public void deleteAll() {
        mParentList.clear();
        notifyDataSetChanged();
    }


    public List<P> getmParentList() {
        return mParentList;
    }

    public void setmParentList(List<P> mParentList) {
        this.mParentList = mParentList;
    }

    protected abstract int bindParentView();

    protected abstract int bindChildrenView();

    protected abstract PV createParentViewHolder();

    protected abstract CV createChildrenViewHolder();

    protected abstract void bindParentData(int position, View convertView, ViewGroup viewGroup,
                                           P p, PV viewHolder);

    protected abstract void bindChildrenData(int groupPosition, int childrenPosition, View convertView,
                                             ViewGroup viewGroup, C c, CV viewHolder);

}
